<template>
  <div class="talk">
    <div v-if="data.talkData.length !== 0" class="talk-list">
      <div class="view-header">
        <div class="tag">
          <b>首页 > 说说</b>
        </div>
        <h1>说说</h1>
      </div>
      <div class="view-body">
        <div
          v-for="(item, index) in data.talkData"
          :key="index"
          class="view-item"
          @click="detail(item.id)"
        >
          <div class="logo">
            <img :src="item.logoPathDesc" alt="" />
          </div>
          <div class="content">
            <div class="user-name">{{ item.userName }}</div>
            <div class="talk-info">
              发布于: {{ item.gmtCreate }}
              <SvgIcon name="talk" class="icon"></SvgIcon>
              {{ item.commentCount }}
            </div>
            <div class="talk-text">{{ item.content }}</div>
            <div class="img-list">
              <div
                v-for="(it, i) in item.imgListDesc"
                :key="i"
                class="img-item"
                @click.stop="
                  dialogVisible = true;
                  dialogImageUrl = it;
                "
              >
                <img :src="it" alt="" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-skeleton v-else :rows="15" animated />
    <div class="talk-right">
      <Myself></Myself>
    </div>
  </div>

  <el-dialog v-model="dialogVisible" :width="data.isPC ? '50%' : '100%'">
    <img class="model-img" w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
</template>

<script setup lang="ts">
import Myself from '@/components/Myself.vue';
import { onMounted, reactive, ref, onUnmounted } from 'vue';
import { _pageTalkList } from '@/pages/talk/talk.service';
import { TalkList } from '@/pages/talk/talk.types';
import { PageDTO } from '@/http/public.type';
import SvgIcon from '@/components/SvgIcon.vue';
import { useRouter } from 'vue-router';

const dialogImageUrl = ref('');
const dialogVisible = ref(false);
const router = useRouter();

const minWidth = 1160;

const data = reactive({
  talkData: [] as TalkList[],
  page: {
    pageNum: 1,
    pageSize: 10
  } as PageDTO,
  isPC: true
});

onMounted(() => {
  initItem();
  handleResize();
  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  window.removeEventListener('resize', handleResize);
});

function initItem() {
  _pageTalkList(data.page).then((res) => {
    data.talkData = res.data.records;
  });
}

function detail(id: string) {
  router.push(`/talk-detail/${id}`);
}

function handleResize() {
  data.isPC = window.innerWidth > minWidth;
}
</script>

<style scoped lang="less">
@media (max-width: 1161px) {
  /* 在宽度小于或等于 1035px 的屏幕上应用以下样式 */
  /* 在这里添加你的样式规则 */
  .pc-container {
    display: none;
  }

  .mobile-container {
    display: block;
  }

  .talk-list {
    width: 100%;
    box-sizing: border-box;
  }

  .talk-right {
    display: none;
  }

  .img-list {
    width: 100%;
    display: flex;
    flex-direction: column;
    padding: 15px 0;

    .img-item {
      width: 100%;
      height: 100%;
      cursor: pointer;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }
}

@media (min-width: 1160px) {
  /* 在宽度大于 1035px 的屏幕上应用以下样式 */
  /* 在这里添加你的样式规则 */
  .pc-container {
    display: block;
  }

  .mobile-container {
    display: none;
  }

  .talk-list {
    flex: 1;
    box-sizing: border-box;
  }

  .talk-right {
    width: 320px;
    margin-left: 20px;
    position: relative;
  }

  .img-list {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    padding: 15px 0;

    .img-item {
      width: 100%;
      height: 100%;
      cursor: pointer;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }
}
.talk {
  width: 78vw;
  z-index: 50;
  margin: 0 auto 120px auto;
  position: relative;
  display: flex;

  .view-body {
    width: 100%;
    display: flex;
    flex-direction: column;

    .view-item {
      width: 100%;
      display: flex;
      background-color: var(--background-secondary);
      color: var(--font-color);
      box-shadow: var(--crane-ring-offset-shadow, 0 0 #0000),
        var(--crane-ring-shadow, 0 0 #0000), var(--crane-shadow);
      border-radius: 15px;
      padding: 15px;
      box-sizing: border-box;
      margin-bottom: 20px;

      .logo {
        width: 50px;
        height: 100%;
        border-radius: 50px;

        img {
          width: 40px;
          height: 40px;
          border-radius: 50px;
        }
      }

      .content {
        flex: 1;
        margin-left: 10px;
        box-sizing: border-box;

        .user-name {
          font-size: 15px;
          font-weight: 800;
        }

        .talk-text {
          margin-top: 10px;
          font-size: 14px;
          white-space: pre-line;
          word-wrap: break-word;
          word-break: break-all;
        }

        .talk-info {
          font-size: 14px;
          color: #999;
          display: flex;
          align-items: center;

          .icon {
            width: 18px;
            height: 18px;
            color: var(--font-color);
            margin-left: 20px;
            margin-right: 5px;
          }
        }
      }
    }
  }

  .view-header {
    width: 100%;

    .writer-info {
      width: 100%;
      display: flex;
    }

    .tag {
      width: 100%;
      position: relative;
      z-index: 1000;
      font-size: 14px;
      display: flex;

      b {
        display: inline-flex;
        font-size: 14px;
        opacity: 0.9;
        padding: 0.125rem;
        --crane-text-opacity: 1;
        color: rgba(255, 255, 255, var(--crane-text-opacity));
        text-transform: uppercase;
        text-shadow: 0 2px 2px rgba(0, 0, 0, 0.5);
      }

      ul {
        padding: 0;
        display: flex;
        margin-top: 0;
        margin-bottom: 0;
        margin-left: 10px;
        justify-content: center;
        align-items: center;

        li {
          font-size: 14px;
          margin-right: 14px;
          opacity: 0.7;
          --crane-text-opacity: 1;
          color: rgba(255, 255, 255, var(--crane-text-opacity));
          text-shadow: 0 2px 2px rgba(0, 0, 0, 0.5);
        }

        em {
          opacity: 0.5;
        }
      }
    }

    h1 {
      margin-top: 15px;
      margin-bottom: 12px;
      font-weight: 800;
      z-index: 100;
      position: relative;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      font-size: 45px;
      text-shadow: 0 2px 2px rgba(0, 0, 0, 0.5);

      --crane-text-opacity: 1;
      color: rgba(255, 255, 255, var(--crane-text-opacity));
    }

    p {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 4;
      -webkit-box-orient: vertical;
      word-wrap: break-word;
      word-break: break-all;
      font-size: 16px;
    }
  }
}

.model-img {
  width: 100%;
  height: 100%;
}
</style>
